package com.bwie.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbDictMapper;
import com.bwie.mapper.TbShopingMapper;
import com.bwie.pojo.TbDict;
import com.bwie.pojo.TbGood;
import com.bwie.pojo.TbShoping;
import com.bwie.service.TbGoodService;
import com.bwie.mapper.TbGoodMapper;
import com.bwie.utils.Result;
import com.bwie.vo.PageGoodVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 28242
* @description 针对表【tb_good】的数据库操作Service实现
* @createDate 2025-06-26 09:50:23
*/
@Service
public class TbGoodServiceImpl extends ServiceImpl<TbGoodMapper, TbGood>
    implements TbGoodService{

    @Autowired
    TbGoodMapper tbGoodMapper;

    @Autowired
    TbDictMapper tbDictMapper;

    @Autowired
    TbShopingMapper tbShopingMapper;

    @Override
    public Result getGoodList(PageGoodVo pageGoodVo) {

        Page<TbGood> page = new Page<>(pageGoodVo.getPageNum(),pageGoodVo.getPageSize());

        QueryWrapper<TbGood> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(TbGood::getSailNumber);
        if (StringUtils.isNotEmpty(pageGoodVo.getGoodName())){
            wrapper.lambda().like(TbGood::getGoodName,pageGoodVo.getGoodName());
        }
        if (pageGoodVo.getGoodBrand()!=null){
            wrapper.lambda().eq(TbGood::getGoodBrand,pageGoodVo.getGoodBrand());
        }
        Page<TbGood> tbGoodPage = tbGoodMapper.selectPage(page, wrapper);

        return Result.ok().setData(tbGoodPage);
    }

    @Override
    public Result getDictByTypeId(Integer dicttypeId) {

        List<TbDict> tbDicts = tbDictMapper.selectList(
                new QueryWrapper<TbDict>().lambda().eq(TbDict::getDictType, dicttypeId)
        );
        return Result.ok().setData(tbDicts);
    }

    @Override
    public Result SaveGoods(TbGood tbGood) {

        String s = RandomUtil.randomNumbers(4);
        tbGood.setGoodCode(s);

        tbGood.setGoodStock(200);
        Integer feight = tbGood.getFeight();
        Integer surety = tbGood.getSurety();
        if (feight==1 && surety==1){
            tbGood.setGoodTag("放心购");
        }else {
            tbGood.setGoodTag("无");
        }
        tbGoodMapper.insert(tbGood);

        return Result.ok();
    }

    @Override
    public Result getShoppingList() {

      List<TbShoping> shopingList=  tbShopingMapper.selectListShopping();

        return Result.ok().setData(shopingList);
    }

    @Override
    public Result getSelectShoping(Integer goodId) {

        TbShoping tbShoping = tbShopingMapper.selectOne(
                new QueryWrapper<TbShoping>().lambda().eq(TbShoping::getGoodId, goodId)
        );
        if (tbShoping==null){
            TbGood tbGood = tbGoodMapper.selectById(goodId);
            TbShoping tbShoping1 = new TbShoping();
            tbShoping1.setGoodId(goodId);
            tbShoping1.setBuyNumber(1);
            tbShoping1.setGoodPrice(tbGood.getGoodPrice());
            tbShoping1.setTotalPrice(tbGood.getGoodPrice());
            tbShopingMapper.insert(tbShoping1);
        }
        tbShoping.setBuyNumber(tbShoping.getBuyNumber()+1);
        tbShoping.setTotalPrice(tbShoping.getBuyNumber()*tbShoping.getGoodPrice());
        tbShopingMapper.updateById(tbShoping);

        return Result.ok();
    }

    @Override
    public Result deletesById(Integer id) {

        tbShopingMapper.deleteById(id);

        return Result.ok();
    }
}




